package netty;

public class Pipeline {
    private volatile ChannelHandlerContext head;
    private volatile ChannelHandlerContext tail;

    public Pipeline() {
        this.head = new ChannelHandlerContext("name", this, 32);
        this.tail = new ChannelHandlerContext("tail", this, 32);
        // 将这连个节点连接起来
        head.next = tail;
        tail.prev = head;
    }

    public void add(String name) {
        ChannelHandlerContext ctx = new ChannelHandlerContext(name, this, 32);
        add(ctx);
    }

    /**
     * 向链表里面添加节点
     *
     * @param newCtx 待添加的新节点
     */
    private void add(ChannelHandlerContext newCtx) {
        ChannelHandlerContext prev = tail.prev;
        newCtx.prev = prev;
        newCtx.next = tail;
        prev.next = newCtx;
        tail.prev = newCtx;
    }

    public void invoke(String msg) {
        ChannelHandlerContext.invokeChannelRead(head, msg);
    }
}
